﻿using System;

namespace Netbits.TelnetServer.Parameters
{
	/// <summary>
	/// Represents a parameter with integer (signed) type.
	/// </summary>
	public sealed class ParameterInt : ParameterBase
	{
		#region Private Variables

		int _Value;
		int _DefaultValue = int.MinValue;
		int _MaxValue = int.MaxValue;
		int _MinValue = int.MinValue;

		#endregion

		#region Constructor

		/// <summary>
		/// Create new instance of this class
		/// </summary>
		/// <param name="Name_">Name of this parameter</param>
		public ParameterInt( string Name_ ) : base( Name_, "INT" )
		{
		}

		#endregion

		#region Public Properties

		/// <summary>
		/// Set or get the minimum value 
		/// </summary>
		public int MinValue
		{
			get
			{
				using( _Lock.Read() )
					return _MinValue;
			}
			set
			{
				using( _Lock.Write() )
					_MinValue = value;
			}
		}

		/// <summary>
		/// Set or get the maximum value
		/// </summary>
		public int MaxValue
		{
			get
			{
				using( _Lock.Read() )
					return _MaxValue;
			}
			set
			{
				using( _Lock.Write() )
					_MaxValue = value;
			}
		}

		/// <summary>
		/// Get the value. 
		/// if no value is set and a default-value is set this are returned. 
		/// </summary>
		public int Value
		{
			get
			{
				if( _HasValue )
					return _Value;
				else if( _HasDefaultValue )
					return _DefaultValue;

				throw new InvalidOperationException( "value not set" );
			}
		}

		/// <summary>
		/// Get or set the default-value
		/// </summary>
		public int DefaultValue
		{
			get
			{
				return _DefaultValue;
			}
			set
			{
				_DefaultValue = value;
				_HasDefaultValue = true;
			}
		}

		#endregion

		#region Overrides

		internal sealed override void CopyFrom( ParameterBase Parameter_ )
		{
			ParameterInt pi = Parameter_ as ParameterInt;
			if( pi == null )
				throw new Exception( "can't copy parameter. parameter is not a type of ParameterInt" );

			_Value = pi._Value;
			_MinValue = pi._MinValue;
			_MaxValue = pi._MaxValue;
			_DefaultValue = pi._DefaultValue;

			base.CopyFrom( Parameter_ );
		}

		internal sealed override void SetValue( string Value_ )
		{
			int value = 0;
			if( !int.TryParse( Value_, out value ) )
				throw new Exception( string.Format( "invalid value '{0}' for parameter '{1}'. value must be an integer", Value_, _Name ) );

			if( value < _MinValue || value > _MaxValue )
				throw new Exception( string.Format( "invalid value '{0}' for parameter '{1}'. value is out of range <{2} and >{3}", Value_, _Name, _MinValue, _MaxValue ) );

			_Value = value;
			base.SetValue( Value_ );
		}

		internal sealed override void ResetValues()
		{
			_Value = _MinValue;
			base.ResetValues();
		}

		internal sealed override void PrintInfo( NetbitsTelnetClientSession Session_, string Search_ )
		{
			string snd = string.Empty;
			if( !string.IsNullOrEmpty( Description ) )
				snd = Description + Environment.NewLine;

			snd += "this parameter need an integer value!";
			Session_.Send( snd );
		}

		#endregion
	}
}
